package weixinOperator

import (
	"fmt"
	"github.com/tidwall/gjson"
	"t11Lib/t11Errors"
	"t11Lib/t11Http"
)

//判断微信用户是否已经绑定网关
func CheckIsUserSubscribed(appId, openId string) (bool, t11Errors.T11Error) {
	ak, err := MpManager.GetAccessToken(appId)
	if err != nil {
		return false, err
	}
	reqUrl := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN", ak, openId)
	body, err := t11Http.Get(reqUrl)
	if err != nil {
		return false, err
	}

	result := gjson.ParseBytes(body).Get("subscribe")
	if result.Exists() {
		return result.Int() == 1, nil
	}
	return false, t11Errors.New("判断用户是否绑定失败")
}
